//
//  PreviewView.swift
//  Gallery-iOS
//
//  Created by qifanxin on 2021/9/23.
//  Copyright © 2021 Hyper Interaktiv AS. All rights reserved.
//
import UIKit
import Foundation

class PreviewView: UIView{
    
    lazy var retryButton : UIButton = self.makeRetryButton()
    
    lazy var confirmButton : UIButton = self.makeConfirmButton()
    
    lazy var previewImageView : UIImageView = self.makePrviewView()
    
    lazy var bottomContainer : UIView = UIView()
    
    let topContainer = UIView()
    var bottomView : UIView = UIView()
    
    var cart: Cart?
    
    required init(cart: Cart) {
        super.init(frame: .zero)
        self.cart = cart
        initView()
    }
    
    override init(frame: CGRect) {
      super.init(frame: frame)

      backgroundColor = UIColor.white
      initView()
    }

    required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }

    func initView() {
        
        
        addSubview(topContainer)
        
        topContainer.addSubview(previewImageView)
        
        bottomView.addSubview(retryButton)
        bottomView.addSubview(confirmButton)
        
        bottomContainer.addSubview(bottomView)
        
        insertSubview(bottomContainer, aboveSubview: topContainer)
        
        topContainer.g_pin(width: UIScreen.main.bounds.size.width)
        topContainer.g_pin(height: UIScreen.main.bounds.size.height)
        previewImageView.contentMode = ContentMode.scaleAspectFill
        previewImageView.clipsToBounds = true
        previewImageView.g_pin(width: UIScreen.main.bounds.size.width)
        previewImageView.g_pin(height: UIScreen.main.bounds.size.height)
        if(!cart!.images.isEmpty) {
            Image.resolve(images: cart!.images, completion: { [weak self](resolvedImages) in
                for (_, resolvedImage) in resolvedImages.enumerated() {
                    let imgData = resolvedImage?.jpegData(compressionQuality: 1.0)
                    let image = UIImage(data: imgData!)
                    self?.previewImageView.image = image?.reSizeImage(reSize: CGSize(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
                }
            })
        }
       
        bottomView.g_pin(on: .top, constant: UIScreen.main.bounds.size.height - 60)
        bottomContainer.g_pin(height: 60)
        bottomContainer.g_pin(width: UIScreen.main.bounds.size.width)
        bottomContainer.g_pinEdges()
        
        previewImageView.g_pinCenter()
        bottomView.g_pin(height: 60)
        bottomView.g_pin(width: UIScreen.main.bounds.size.width)
        bottomView.backgroundColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 0.65)
        
        retryButton.g_pin(on: .leftMargin, constant: 15)
        retryButton.g_pin(on: .centerY)
        confirmButton.g_pin(on: .rightMargin, constant: -15)
        confirmButton.g_pin(on: .centerY)
        
    }
    
    func makePrviewView() -> UIImageView {
        let imageView = UIImageView()
        return imageView
    }
    
    func makeRetryButton() -> UIButton {
        let button = UIButton(type: .system)
        button.setTitle("重拍", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 1).withSize(16)
        button.frame = CGRect.init(x: 0, y: 0, width: 200, height: 30)
        return button
    }
    
    func makeConfirmButton() -> UIButton {
        let button = UIButton(type: .system)
        button.setTitle("使用照片", for: UIControl.State.normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 1).withSize(16)
        button.frame = CGRect.init(x: 0, y: 0, width: 200, height: 30)
        return button
    }
    
}

extension  UIImage  {
     /**
      *  重设图片大小
      */
     func  reSizeImage(reSize: CGSize )-> UIImage  {
         //UIGraphicsBeginImageContext(reSize);
         UIGraphicsBeginImageContextWithOptions (reSize, false , UIScreen.main.scale);
         self.draw(in: CGRect.init(x: 0, y: 0, width: reSize.width, height: reSize.height));
         let reSizeImage: UIImage = UIGraphicsGetImageFromCurrentImageContext ()!;
         UIGraphicsEndImageContext ();
         return  reSizeImage;
     }
     
}
